﻿Imports System.Windows.Forms

Public Class CodeList
    Private Sub CodeList_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CodeList_Handle = Me.Handle
        TextBox1.Left = 0
        TextBox1.Width = 300
        ListBox2.Left = 0
        ListBox2.Width = 25
        ListBox2.Height = (292 + ListBox2.ItemHeight)
        ListBox1.Left = 25
        ListBox1.Width = 275
        ListBox1.Height = (292 + ListBox1.ItemHeight)
        RichTextBox1.Left = 150
        RichTextBox1.Width = 300
        RichTextBox1.Height = 292
        For i = 1 To UBound(CodeArr)
            ListBox1.Items.Add(CodeArr(i, 2))
            If i < 11 Then ListBox2.Items.Add(i)
        Next

        'Debug.Print(My.Computer.Screen.Bounds.Width.ToString())
        'Debug.Print(My.Computer.Screen.Bounds.Height.ToString())


    End Sub

    Private Sub CodeList_LostFocus(sender As Object, e As EventArgs) Handles Me.LostFocus
        '/失去焦点就隐藏
        ' Me.Hide()

    End Sub

    'Protected Overrides Function ProcessDialogKey(keyData As Keys) As Boolean
    '    If keyData = Keys.Up OrElse keyData = Keys.Down OrElse keyData = Keys.Left OrElse keyData = Keys.Right Then
    '        Return False
    '    Else
    '        Return MyBase.ProcessDialogKey(keyData)
    '    End If
    'End Function

    Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
        '’ Private Sub ListBox1_Click(sender As Object, e As EventArgs) Handles ListBox1.Click
        Dim SelInfo As SelLineColInfo = VBGetSelection() '获取光标所在位置
        Dim KeyStr As String = RTrim(_VBE.ActiveCodePane.CodeModule.Lines(SelInfo.SLine, 1)) '获取当前行数据

        Dim arr As Object = Split(KeyStr, " ") '利用空格写入数组
        Dim tempstr As String
        If UBound(arr) = 0 Then
            For i = 0 To UBound(arr)
                tempstr = ""
            Next
        Else
            For i = 0 To UBound(arr) - 1
                tempstr = tempstr & " " & arr(i)
            Next
        End If
        _VBE.ActiveCodePane.CodeModule.ReplaceLine(SelInfo.SLine, Trim(tempstr & Replace(Replace(Me.RichTextBox1.Text, "{", ""), "}", ""))) '利用替换行的方法，写入代码
        SetWindowPos(CodeList_Handle, 0, 0, 0, 0, 0, SWP_HIDEWINDOW) '隐藏窗口
        SetFocus(ActiveCodePane_Handle) '获取焦点
        '//开始设置选中位置，已{}作为特征分隔符
        'Dim SelCode As String = _VBE.ActiveCodePane.CodeModule.Lines(SelInfo.SLine, SelInfo.ELine)
        If InStr(Me.RichTextBox1.Text, "{") > 0 And InStr(Me.RichTextBox1.Text, "}") > 0 Then
            _VBE.ActiveCodePane.SetSelection(SelInfo.SLine + GetSLine(Me.RichTextBox1.Text), GetSCol(tempstr & Me.RichTextBox1.Text), SelInfo.ELine + GetELine(Me.RichTextBox1.Text), GetECol(tempstr & Me.RichTextBox1.Text))
            '_VBE.ActiveCodePane.SetSelection(SelInfo.SLine + GetSLine(Me.RichTextBox1.Text), GetSCol(SelCode), SelInfo.ELine + GetELine(Me.RichTextBox1.Text), GetECol(SelCode))
        Else
            _VBE.ActiveCodePane.SetSelection(SelInfo.SLine, Len(tempstr & Me.RichTextBox1.Text), SelInfo.SLine, Len(tempstr & Me.RichTextBox1.Text))
        End If
        IndentModule()
    End Sub

    'Private Sub ListBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseMove
    '    Try
    '        ListBox1.SelectedIndex = ListBox1.IndexFromPoint(e.Location)
    '    Catch

    '    End Try


    '    ' Try
    '    '  Dim pos As Integer
    '    '  pos = e.X / Screen.PrimaryScreen.Bounds.Width + e.Y / Screen.PrimaryScreen.Bounds.Height * 65536
    '    ' If pos <= 25887 And pos >= 0 Then
    '    '     ListBox1.SelectedIndex = ListBox1.TopIndex + pos \ 1500
    '    '     RichTextBox1.Text = DCodeArr.Item(ListBox1.SelectedItem.ToString)
    '    '  End If
    '    'Catch

    '    'End Try
    'End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Try
            If ListBox1.SelectedItem Is Nothing Then Exit Sub
            RichTextBox1.Text = DCodeArr.Item(ListBox1.SelectedItem.ToString)
        Catch

        End Try
    End Sub



    Private Sub ListBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ListBox1.KeyPress
        Dim SelInfo As SelLineColInfo = VBGetSelection() '获取光标所在位置
        Dim KeyStr As String = RTrim(_VBE.ActiveCodePane.CodeModule.Lines(SelInfo.SLine, 1)) '获取当前行数据
        Dim arr As Object = Split(KeyStr, " ") '利用空格写入数组
        Dim tempstr As String = ""
        Dim xb As Int16 = 0
        If UBound(arr) = 0 Then
            For i = 0 To UBound(arr)
                tempstr = ""
            Next
        Else
            For i = 0 To UBound(arr) - 1
                tempstr = tempstr & " " & arr(i)
            Next
        End If
        If e.KeyChar = Chr(27) Then 'ESC，直接隐藏窗体
            Me.Hide()
            ''  _VBE.ActiveCodePane.SetSelection(SelInfo.SLine, Len(tempstr), SelInfo.SLine, Len(tempstr) + 1) '返回光标位置
            'ESC_TN = True
            'SetFocus(ActiveHandle)
            ''//开始设置选中位置，已{}作为特征分隔符
            'Dim SelCode As String = _VBE.ActiveCodePane.CodeModule.Lines(SelInfo.SLine, SelInfo.ELine)
            'If InStr(Me.RichTextBox1.Text, "{") > 0 And InStr(Me.RichTextBox1.Text, "}") > 0 Then
            '    _VBE.ActiveCodePane.SetSelection(SelInfo.SLine + GetSLine(Me.RichTextBox1.Text), GetSCol(tempstr & Me.RichTextBox1.Text), SelInfo.ELine + GetELine(Me.RichTextBox1.Text), GetECol(tempstr & Me.RichTextBox1.Text))
            '    '_VBE.ActiveCodePane.SetSelection(SelInfo.SLine + GetSLine(Me.RichTextBox1.Text), GetSCol(SelCode), SelInfo.ELine + GetELine(Me.RichTextBox1.Text), GetECol(SelCode))
            'Else
            '    _VBE.ActiveCodePane.SetSelection(SelInfo.SLine, Len(tempstr & Me.RichTextBox1.Text), SelInfo.SLine, Len(tempstr & Me.RichTextBox1.Text))
            'End If
        ElseIf e.KeyChar = Chr(13) Then '回车键，直接写代码到代码编辑区
            _VBE.ActiveCodePane.CodeModule.ReplaceLine(SelInfo.SLine, Trim(tempstr & Replace(Replace(Me.RichTextBox1.Text, "{", ""), "}", ""))) '利用替换行的方法，写入代码
            SetWindowPos(CodeList_Handle, 0, 0, 0, 0, 0, SWP_HIDEWINDOW) '隐藏窗口
            SetFocus(ActiveCodePane_Handle) '获取焦点
            '//开始设置选中位置，已{}作为特征分隔符
            'Dim SelCode As String = _VBE.ActiveCodePane.CodeModule.Lines(SelInfo.SLine, SelInfo.ELine)
            If InStr(Me.RichTextBox1.Text, "{") > 0 And InStr(Me.RichTextBox1.Text, "}") > 0 Then
                _VBE.ActiveCodePane.SetSelection(SelInfo.SLine + GetSLine(Me.RichTextBox1.Text), GetSCol(tempstr & Me.RichTextBox1.Text), SelInfo.ELine + GetELine(Me.RichTextBox1.Text), GetECol(tempstr & Me.RichTextBox1.Text))
                '_VBE.ActiveCodePane.SetSelection(SelInfo.SLine + GetSLine(Me.RichTextBox1.Text), GetSCol(SelCode), SelInfo.ELine + GetELine(Me.RichTextBox1.Text), GetECol(SelCode))
            Else
                _VBE.ActiveCodePane.SetSelection(SelInfo.SLine, Len(tempstr & Me.RichTextBox1.Text), SelInfo.SLine, Len(tempstr & Me.RichTextBox1.Text) + 1)
            End If
            IndentModule()
        ElseIf e.KeyChar = Chr(8) Then '删除键
            TextBox1.SelectionStart = TextBox1.Text.Length   '文本的选取长度
            TextBox1.ScrollToCaret()  '关键之语句：将焦点滚动到文本内容后
            TextBox1.Focus()
        ElseIf e.KeyChar = Chr(48) Then '0
            xb = 10 - 1 '0表示10
            If xb <= sjgs Then
                SRNR输入(xb)
            Else
                Me.Hide()
            End If
        ElseIf e.KeyChar = Chr(49) Then '1
            xb = 1 - 1 '表示1
            If xb <= sjgs Then
                SRNR输入(xb)
            Else
                Me.Hide()
            End If
        ElseIf e.KeyChar = Chr(50) Then '2
            xb = 2 - 1 '表示1
            If xb <= sjgs Then
                SRNR输入(xb)
            Else
                Me.Hide()
            End If
        ElseIf e.KeyChar = Chr(51) Then '3
            xb = 3 - 1 '表示1
            If xb <= sjgs Then
                SRNR输入(xb)
            Else
                Me.Hide()
            End If
        ElseIf e.KeyChar = Chr(52) Then '4
            xb = 4 - 1 '表示1
            If xb <= sjgs Then
                SRNR输入(xb)
            Else
                Me.Hide()
            End If
        ElseIf e.KeyChar = Chr(53) Then '5
            xb = 5 - 1 '表示1
            If xb <= sjgs Then
                SRNR输入(xb)
            Else
                Me.Hide()
            End If
        ElseIf e.KeyChar = Chr(54) Then '6
            xb = 6 - 1 '表示1
            If xb <= sjgs Then
                SRNR输入(xb)
            Else
                Me.Hide()
            End If
        ElseIf e.KeyChar = Chr(55) Then '7
            xb = 7 - 1 '表示1
            If xb <= sjgs Then
                SRNR输入(xb)
            Else
                Me.Hide()
            End If
        ElseIf e.KeyChar = Chr(56) Then '8
            xb = 8 - 1 '表示1
            If xb <= sjgs Then
                SRNR输入(xb)
            Else
                Me.Hide()
            End If
        ElseIf e.KeyChar = Chr(57) Then '9
            xb = 9 - 1 '表示1
            If xb <= sjgs Then
                SRNR输入(xb)
            Else
                Me.Hide()
            End If



        Else
            Me.Hide()
        End If
    End Sub

    ''' <summary>
    ''' 输入某下标
    ''' </summary>
    Sub SRNR输入(Optional x As Int16 = 0)
        Dim SelInfo As SelLineColInfo = VBGetSelection() '获取光标所在位置
        Dim KeyStr As String = RTrim(_VBE.ActiveCodePane.CodeModule.Lines(SelInfo.SLine, 1)) '获取当前行数据
        Dim arr As Object = Split(KeyStr, " ") '利用空格写入数组
        Dim tempstr As String = ""

        If UBound(arr) = 0 Then
            For i = 0 To UBound(arr)
                tempstr = ""
            Next
        Else
            For i = 0 To UBound(arr) - 1
                tempstr = tempstr & " " & arr(i)
            Next
        End If
        If x > sjgs Then Return
        ListBox1.SelectedIndex = x
        _VBE.ActiveCodePane.CodeModule.ReplaceLine(SelInfo.SLine, Trim(tempstr & Replace(Replace(Me.RichTextBox1.Text, "{", ""), "}", ""))) '利用替换行的方法，写入代码
        SetWindowPos(CodeList_Handle, 0, 0, 0, 0, 0, SWP_HIDEWINDOW) '隐藏窗口
        SetFocus(ActiveCodePane_Handle) '获取焦点
        '//开始设置选中位置，已{}作为特征分隔符
        'Dim SelCode As String = _VBE.ActiveCodePane.CodeModule.Lines(SelInfo.SLine, SelInfo.ELine)
        If InStr(Me.RichTextBox1.Text, "{") > 0 And InStr(Me.RichTextBox1.Text, "}") > 0 Then
            _VBE.ActiveCodePane.SetSelection(SelInfo.SLine + GetSLine(Me.RichTextBox1.Text), GetSCol(tempstr & Me.RichTextBox1.Text), SelInfo.ELine + GetELine(Me.RichTextBox1.Text), GetECol(tempstr & Me.RichTextBox1.Text))
            '_VBE.ActiveCodePane.SetSelection(SelInfo.SLine + GetSLine(Me.RichTextBox1.Text), GetSCol(SelCode), SelInfo.ELine + GetELine(Me.RichTextBox1.Text), GetECol(SelCode))
        Else
            _VBE.ActiveCodePane.SetSelection(SelInfo.SLine, Len(tempstr & Me.RichTextBox1.Text), SelInfo.SLine, Len(tempstr & Me.RichTextBox1.Text) + 1)
        End If
        IndentModule()
    End Sub



    Function GetSCol(a) As Int16
        Dim trr As Object = Split(a, vbLf)
        For i = 0 To UBound(trr)
            Dim s As Int16 = InStr(trr(i), "{")
            If s > 0 Then
                GetSCol = System.Text.Encoding.Default.GetByteCount(Mid(trr(i), 1, s))
                Exit Function
            End If
        Next
        Return GetSCol
    End Function

    Function GetECol(a) As Int16
        Dim trr As Object = Split(a, vbLf)
        For i = 0 To UBound(trr)

            Dim s As Int16 = InStr(trr(i), "}")
            If s > 0 Then
                GetECol = System.Text.Encoding.Default.GetByteCount(Mid(trr(i), 1, s)) - 1
                Exit Function
            End If
            'For j = 1 To System.Text.Encoding.Default.GetByteCount(trr(i))
            'If Mid(trr(i), j, 1) = "}" Then
            'GetECol = j
            'Exit Function
            'End If
            'Next
        Next
        Return GetECol
    End Function

    Function GetSLine(a) As Int16
        Dim trr As Object = Split(a, vbLf)
        For i = 0 To UBound(trr)
            If InStr(trr(i), "{") Then
                GetSLine = i
                Exit Function
            End If
        Next
        Return GetSLine
    End Function

    Function GetELine(a) As Int16
        Dim trr As Object = Split(a, vbLf)
        For i = 0 To UBound(trr)
            If InStr(trr(i), "}") Then
                GetELine = i
                Exit Function
            End If
        Next
        Return GetELine
    End Function

    'Private Sub CodeList_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    '    MsgBox(e.KeyCode.ToString)
    'End Sub

    Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged

    End Sub
    Private Sub CodeList_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        Me.ListBox1.Focus()
        ListBox1.SelectedIndex = 0
        'TextBox1.SelectionStart = TextBox1.Text.Length   '文本的选取长度
        'TextBox1.ScrollToCaret()  '关键之语句：将焦点滚动到文本内容后
        'TextBox1.Focus()
    End Sub
    Dim sjgs As Int16 = -1
    Private Sub R更新窗体数据()
        '//更新窗体数据
        Dim KeyStr As String = Trim(Me.TextBox1.Text) '获取当前行数据
        Dim arr As Object = Split(KeyStr, " ") '利用空格写入数组
        KeyStr = UCase(arr(UBound(arr))) '转换为大写，方便匹对
        '/刷新Listbox1的列表
        ListBox1.Items.Clear()
        Dim brr() As Object = Nothing  '动态数组，存储临时匹配结果
        sjgs = -1
        For X = 1 To UBound(CodeArr)
            If UCase(CodeArr(X, 2)) Like "*" & KeyStr & "*" Then
                sjgs = sjgs + 1
                ReDim Preserve brr(0 To sjgs)
                brr(sjgs) = CodeArr(X, 2)

            End If
        Next
        If sjgs >= 0 Then '匹配到结果的时候才进行写入，匹配不到不写
            ListBox1.Items.AddRange(brr)
            '//获取光标屏幕坐标，移动窗口
            '’ ListBox1.SelectedIndex = 0
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Me.Hide()
        OpenExCODE()
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        DCodeArr.Clear()
        ReadExCODE()
    End Sub
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        R更新窗体数据()
    End Sub
    Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim SelInfo As SelLineColInfo = VBGetSelection() '获取光标所在位置
        Dim KeyStr As String = RTrim(_VBE.ActiveCodePane.CodeModule.Lines(SelInfo.SLine, 1)) '获取当前行数据
        Dim arr As Object = Split(KeyStr, " ") '利用空格写入数组
        Try
            If e.KeyChar = Chr(27) Then 'ESC，直接隐藏窗体
                Me.Hide()
                '’ _VBE.ActiveCodePane.SetSelection(SelInfo.SLine, SelInfo.SCol, SelInfo.ELine, SelInfo.ECol + 1) '返回光标位置
            ElseIf e.KeyChar = Chr(13) Then '回车键，直接写代码到代码编辑区
                R更新窗体数据()
                Me.ListBox1.Focus()
                ListBox1.SelectedIndex = 0
                'ElseIf e.KeyChar = Chr(40) Then '
                '    R更新窗体数据()
                '    Me.ListBox1.Focus()
                '    ListBox1.SelectedIndex = 0
            End If
        Catch ex As Exception
            '’_VBE.ActiveCodePane.SetSelection(SelInfo.SLine, SelInfo.SCol, SelInfo.ELine, SelInfo.ECol + 1) '返回光标位置
        End Try
    End Sub
End Class